package com.market.plus.core.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

@Entity
@Table(name = "TRIBE")
@NamedQueries( {
      @NamedQuery(name = Tribe.FIND_ALL, query = "SELECT tribe FROM Tribe tribe"),
      @NamedQuery(name = Tribe.FIND_GUID, query = "SELECT tribe FROM Tribe tribe WHERE tribe.tribeGUID=:guid"),
      @NamedQuery(name = Tribe.FIND_NEWS, query = "SELECT tribe.news FROM Tribe tribe WHERE tribe.tribeGUID=:guid")})
public class Tribe implements Persistable, StatusInfo, ModifyedInfo {

   public static final String FIND_ALL = "TRIBE.FIND_ALL";

   public static final String FIND_GUID = "TRIBE.FIND_GUID";

   public static final String FIND_NEWS = "TRIBE.FIND_NEWS";

   public static final String PARA_GUID = "guid";

   @Id
   @GeneratedValue(strategy = GenerationType.TABLE, generator = "GEN_TRIBE")
   @TableGenerator(name = "GEN_TRIBE", table = "SEQUENCE_LIST", allocationSize = 1, initialValue = 1, pkColumnName = "NAME", valueColumnName = "NEXT_SEQUENCE", pkColumnValue = "TRIBE")
   @Column(name = "TRIBE_ID")
   private Long id;

   @Column(name = "TRIBE_NAME", nullable = false)
   private String name;

   @Column(name = "DESCRIPTION")
   private String description;

   @Column(name = "TRIBE_GUID", nullable = false)
   private String tribeGUID;

   @Column(name = "CREATED_USER")
   private String createdUser;

   @Column(name = "CREATED_DATE")
   private Date createdDate;

   @Column(name = "MODIFYED_USER")
   private String modifyedUser;

   @Column(name = "MODIFYED_DATE")
   private Date modifyedDate;

   @Column(name = "STATUS")
   @Enumerated(EnumType.ORDINAL)
   private Status status;

   @OneToOne
   @JoinColumn(name = "PARTICIPANT_ID")
   private Participant participant;

   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="AVATAR_ID")
   private Avatar avatar;

   @ManyToMany(cascade=CascadeType.ALL)
   @JoinTable(name = "TRIBE_NEWS", joinColumns = @JoinColumn(name = "TRIBE_NEWS_ID", referencedColumnName = "TRIBE_ID"), 
         inverseJoinColumns = @JoinColumn(name = "NEWS_TRIBE_ID", referencedColumnName = "NEWS_ID"))
   private Set<News> news;

   public Tribe() {
      this.news = new HashSet<News>();
   }

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getCreatedUser() {
      return createdUser;
   }

   public void setCreatedUser(String createdUser) {
      this.createdUser = createdUser;
   }

   public String getModifyedUser() {
      return modifyedUser;
   }

   public void setModifyedUser(String modifyedUser) {
      this.modifyedUser = modifyedUser;
   }

   public Date getCreatedDate() {
      return createdDate;
   }

   public void setCreatedDate(Date createdDate) {
      this.createdDate = createdDate;
   }

   public Date getModifyedDate() {
      return modifyedDate;
   }

   public void setModifyedDate(Date modifyedDate) {
      this.modifyedDate = modifyedDate;
   }

   public Status getStatus() {
      return status;
   }

   public void setStatus(Status status) {
      this.status = status;
   }

   public Participant getParticipant() {
      return participant;
   }

   public void setParticipant(Participant participant) {
      this.participant = participant;
   }

   public Avatar getAvatar() {
      return avatar;
   }

   public void setAvatar(Avatar avatar) {
      this.avatar = avatar;
   }

   public String getTribeGUID() {
      return tribeGUID;
   }

   public void setTribeGUID(String tribeGUID) {
      this.tribeGUID = tribeGUID;
   }

   public String getDescription() {
      return description;
   }

   public void setDescription(String description) {
      this.description = description;
   }

   public void addNews(News news){
      this.news.add(news);
   }

   public List<News> getNews() {
      return new ArrayList<News>(news);
   }

   public void setNews(Set<News> news) {
      this.news.clear();
      this.news.addAll(news);
   }

   @Override
   public boolean equals(Object obj) {
      Tribe propertyCompare = (Tribe) obj;
      if (this.getId().longValue() == propertyCompare.getId().longValue()) {
         return true;
      }
      return false;
   }
}
